#include "collectmoney.h"
#include "ui_collectmoney.h"

#include <QString>
#include <QMessageBox>
#include <QPalette>

int globleCheck = 0;
int globalRow = 0;
double globalAllMoney = 0;

collectMoney::collectMoney(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::collectMoney)
{
    ui->setupUi(this);

    //QPixmap pixmap = QPixmap("D://Qt//workspace//bookManager//pictures//xiyang.jpg").scaled(this->size());
    QPixmap pixmap = QPixmap("D://CsSoftware//Qt//__MyFiles//workspace//bookManager//pictures//flower.jpg").scaled(this->size());
    QPalette palette(this->palette());
    palette.setBrush(QPalette::Background,pixmap);
    this->setPalette(palette);


    this->model_view = new QStandardItemModel;//new出来一个表格模型
    this->model_cal = new QStandardItemModel;//new出来一个表格模型

    //设置view表格
    this->model_view->setHorizontalHeaderItem(0,new QStandardItem("数量"));//QStandardItem是单元格
    this->model_view->setHorizontalHeaderItem(1,new QStandardItem("ISBN号"));
    this->model_view->setHorizontalHeaderItem(2,new QStandardItem("书名"));
    this->model_view->setHorizontalHeaderItem(3,new QStandardItem("单价"));
    this->model_view->setHorizontalHeaderItem(4,new QStandardItem("金额"));

    //让view显示视图
    this->ui->tableView->setModel(model_view);

    //设置单元格宽度
    this->ui->tableView->setColumnWidth(0,70);
    this->ui->tableView->setColumnWidth(1,100);
    this->ui->tableView->setColumnWidth(2,150);
    this->ui->tableView->setColumnWidth(3,70);
    this->ui->tableView->setColumnWidth(3,70);


    //设置cal表格
    this->model_cal->setVerticalHeaderItem(0,new QStandardItem("销售合计"));//QStandardItem是单元格
    this->model_cal->setVerticalHeaderItem(1,new QStandardItem("零售税"));
    this->model_cal->setVerticalHeaderItem(2,new QStandardItem("应付总额"));

    this->ui->tableView_2->setModel(model_cal);

    this->ui->tableView_2->setRowHeight(0,35);
    this->ui->tableView_2->setRowHeight(1,35);
    this->ui->tableView_2->setRowHeight(2,35);

}

collectMoney::~collectMoney()
{
    delete ui;
}

void collectMoney::on_btn_search_clicked()
{

//    //设置view表格
//    this->model_view->setHorizontalHeaderItem(0,new QStandardItem("数量"));//QStandardItem是单元格
//    this->model_view->setHorizontalHeaderItem(1,new QStandardItem("ISBN号"));
//    this->model_view->setHorizontalHeaderItem(2,new QStandardItem("书名"));
//    this->model_view->setHorizontalHeaderItem(3,new QStandardItem("单价"));
//    this->model_view->setHorizontalHeaderItem(4,new QStandardItem("金额"));

//    //让view显示视图
//    this->ui->tableView->setModel(model_view);

//    //设置单元格宽度
//    this->ui->tableView->setColumnWidth(0,70);
//    this->ui->tableView->setColumnWidth(1,100);
//    this->ui->tableView->setColumnWidth(2,150);
//    this->ui->tableView->setColumnWidth(3,70);
//    this->ui->tableView->setColumnWidth(3,70);

    int check = 0;

    for(int i = 0;i < 100;i++){
        QString InputIsbn = this->ui->le_isbnInput->text();

        if(InputIsbn == dataArray[i].isbn&&dataArray[i].existOrNot){
            check = 1;//1表示该书存在

            if(dataArray[i].qtyOnHand != "0"){
                QString content = "ISBN:" + dataArray[i].isbn + '\n' + "书名：" + dataArray[i].bookTitle + '\n' + "作者：" + dataArray[i].author + '\n' + "出版社：" + dataArray[i].publisher
                        + '\n' + "进书日期：" + dataArray[i].dataAdded + '\n' + "库存量：" + dataArray[i].qtyOnHand + '\n' + "批发价：" + dataArray[i].wholesale + '\n' + "零售价：" + dataArray[i].retail
                        + '\n' + '\n' + "请选择购买数量！";

                int ret = QMessageBox::question(this,"请确认信息",content,"确认","取消");
//                int row = 0;
                if(ret == 0){
                    check = 2;//2表示该书存在且有库存
//                    globleCheck = check;

//                    QString number = this->ui->le_number->text();
//                    this->model_view->setItem(globalRow,0,new QStandardItem(number));
//                    this->model_view->setItem(globalRow,1,new QStandardItem(dataArray[i].isbn));
//                    this->model_view->setItem(globalRow,2,new QStandardItem(dataArray[i].bookTitle));
//                    this->model_view->setItem(globalRow,3,new QStandardItem(dataArray[i].retail));
//                    double pay = dataArray[i].retail.toDouble()*number.toDouble();
//                    this->model_view->setItem(globalRow,4,new QStandardItem(pay));

//                    globalRow++;

                }

            }else{
                QMessageBox::critical(this,"错误","该书存在但已无库存!请选择其他图书！","确定");
                break;//此时check为1
            }
        }
    }

    if(check == 0) QMessageBox::critical(this,"错误","该书不存在！","确定");
}

void collectMoney::on_btn_sure_clicked()
{
//    //设置view表格
//    this->model_view->setHorizontalHeaderItem(0,new QStandardItem("数量"));//QStandardItem是单元格
//    this->model_view->setHorizontalHeaderItem(1,new QStandardItem("ISBN号"));
//    this->model_view->setHorizontalHeaderItem(2,new QStandardItem("书名"));
//    this->model_view->setHorizontalHeaderItem(3,new QStandardItem("单价"));
//    this->model_view->setHorizontalHeaderItem(4,new QStandardItem("金额"));

//    //让view显示视图
//    this->ui->tableView->setModel(model_view);

//    //设置单元格宽度
//    this->ui->tableView->setColumnWidth(0,70);
//    this->ui->tableView->setColumnWidth(1,100);
//    this->ui->tableView->setColumnWidth(2,150);
//    this->ui->tableView->setColumnWidth(3,70);
//    this->ui->tableView->setColumnWidth(3,70);

    //设置cal表格
    this->model_cal->clear();//清空上一次搜索显示

    //设置cal表格
    this->model_cal->setVerticalHeaderItem(0,new QStandardItem("销售合计"));//QStandardItem是单元格
    this->model_cal->setVerticalHeaderItem(1,new QStandardItem("零售税"));
    this->model_cal->setVerticalHeaderItem(2,new QStandardItem("应付总额"));

    this->ui->tableView_2->setModel(model_cal);

    this->ui->tableView_2->setRowHeight(0,40);
    this->ui->tableView_2->setRowHeight(1,40);
    this->ui->tableView_2->setRowHeight(2,40);

    QString InputIsbn = this->ui->le_isbnInput->text();
    QString number = this->ui->le_number->text();

    double pay = 0;
    for(int i = 0;i < 100;i++){
        if(InputIsbn == dataArray[i].isbn){
            this->model_view->setItem(globalRow,0,new QStandardItem(number));//显示购买数量
            int remain = dataArray[i].qtyOnHand.toInt()-number.toInt();
            QString Remain = QString::number(remain);
            dataArray[i].qtyOnHand = Remain;//该次购买后库存的剩余量

            this->model_view->setItem(globalRow,1,new QStandardItem(dataArray[i].isbn));
            this->model_view->setItem(globalRow,2,new QStandardItem(dataArray[i].bookTitle));
            this->model_view->setItem(globalRow,3,new QStandardItem(dataArray[i].retail));
            //显示金额Pay
            pay = dataArray[i].retail.toDouble()*number.toInt();
            QString Pay = QString::number(pay,'f',2);
            this->model_view->setItem(globalRow,4,new QStandardItem(Pay));

            globalRow++;
            //以上，view表格的显示结束，下面进行cal表格的显示

            //销售合计 = 所有的pay累加
            globalAllMoney = globalAllMoney + pay;
            QString ALLMoney = QString::number(globalAllMoney,'f',2);
            this->model_cal->setItem(0,0,new QStandardItem(ALLMoney));

            //零售税 = 总金额*dataArray[i].tex
            double retail_tex = globalAllMoney * dataArray[i].tex.toDouble();
            QString R_tex = QString::number(retail_tex,'f',2);
            this->model_cal->setItem(1,0,new QStandardItem(R_tex));

            //应付总额 = 销售合计+零售税
            double should_pay = retail_tex + globalAllMoney;
            QString S_pay = QString::number(should_pay,'f',2);
            this->model_cal->setItem(2,0,new QStandardItem(S_pay));
        }
    }
}

void collectMoney::on_btn_over_clicked()
{
    this->close();
}
